var myChart = null;

function setMyChartOption(myChart, tmp) {
    myChart.setOption(tmp, true);
}

function autoMargin(myChart) {
//用于使chart自适应高度和宽度
    window.onresize = function () {
        myChart.resize();
    };
}

function splitCoordinate(tmpdata) {
    var x = [];
    var y = [];
    for (var i = 0; i < tmpdata.points.length; i++) {
        x.push(tmpdata.points[i].x);
        y.push(tmpdata.points[i].y);
    }
    return {
        x: x,
        y: y
    };
}

function getOption(data1) {
    var option = {
        title: {
            text: '光谱数据分析'
        },
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'cross'
            }
        },
        legend: {
            // data: ['光谱1', '光谱2']
            data: ['光谱1']
        },
        toolbox: {
            show: true,
            feature: {
                dataZoom: {
                    yAxisIndex: 'none'
                },
                dataView: {readOnly: false},
                magicType: {type: ['line', 'bar']},
                restore: {},
                saveAsImage: {}
            }
        },
        xAxis: {
            type: 'category',
            boundaryGap: false,
            data: data1.x
        },
        yAxis: {
            type: 'value',
            axisLabel: {
                formatter: '{value} OD'
            }
        },
        series: [
            {
                name: '光谱1',
                type: 'line',
                smooth: true,
                data: data1.y,
                markPoint: {
                    data: [
                        {type: 'max', name: '最大值'},
                        {type: 'min', name: '最小值'}
                    ]
                },
                markLine: {
                    data: [
                        {type: 'average', name: '平均值'}
                    ]
                }
            }
            /*{
                name: '光谱2',
                type: 'line',
                smooth: true,
                data: data2.y,
                markPoint: {
                    data: [
                        {type: 'max', name: '最大值'},
                        {type: 'min', name: '最小值'}
                    ]
                },
                markLine: {
                    data: [
                        {type: 'average', name: '平均值'}
                    ]
                }
            }*/
        ]
    };
    return option;
}

function strategy(spectrum) {
    myChart = echarts.init(document.getElementById('echarts'));
    var data = splitCoordinate(spectrum);
    var option = getOption(data);
    setMyChartOption(myChart, option);
    autoMargin(myChart);
}

$(document).ready(function () {
    var params = {
        spectrumID: 1,
        pretreatmentID: 1,
        analysisID: 2,
        standardSpectrumID: 1
    };
    $.ajax({
        url: "/spectrumAnalysis.do",
        async: true,
        type: 'post',
        data: params,
        success: function (result) {
            // alert(result);
            strategy(result.spectrumFile);
            $("#queryReport").attr("href", "queryReport.do?spectruminfoID=" + result.spectrumID);
        }
    });
});

function exportReport() {
    var opt = {
        opts: {
            // 导出的格式，可选 png, jpeg
            type: 'png',
            // 导出的图片分辨率比例，默认为 1。
            pixelRatio: 2,
            // 导出的图片背景色，默认使用 option 里的 backgroundColor
            //backgroundColor: string,
            // 忽略组件的列表，例如要忽略 toolbox 就是 ['toolbox']
            // excludeComponents: Array.<string>
        }
    };
    var echartsIMG = {
        name: 'echartsIMG',
        value: myChart.getDataURL("png")
    };
    var data = $('#spectrumAnalysisFrom').serializeArray();
    data.push(echartsIMG);
    $.ajax({
        url: "/exportSpectrumImage.do",
        async: true,
        type: 'post',
        data: data,
        success: function (result) {
            alert(result);
            // strategy(result.spectrumFile);
        }
    });
}